﻿using System;
using System.Collections.Generic;
using GeoAPI.Geometries;
using NetTopologySuite.Algorithm;

namespace NetTopologySuite.Operation.Predicate
{
	public class SegmentIntersectionTester
	{
		public bool HasIntersectionWithLineStrings(ICoordinateSequence seq, ICollection<IGeometry> lines)
		{
			foreach (IGeometry geometry in lines)
			{
				ILineString lineString = (ILineString)geometry;
				this.HasIntersection(seq, lineString.CoordinateSequence);
				if (this._hasIntersection)
				{
					break;
				}
			}
			return this._hasIntersection;
		}

		public bool HasIntersection(ICoordinateSequence seq0, ICoordinateSequence seq1)
		{
			int num = 1;
			while (num < seq0.Count && !this._hasIntersection)
			{
				seq0.GetCoordinate(num - 1, this.pt00);
				seq0.GetCoordinate(num, this.pt01);
				int num2 = 1;
				while (num2 < seq1.Count && !this._hasIntersection)
				{
					seq1.GetCoordinate(num2 - 1, this.pt10);
					seq1.GetCoordinate(num2, this.pt11);
					this.li.ComputeIntersection(this.pt00, this.pt01, this.pt10, this.pt11);
					if (this.li.HasIntersection)
					{
						this._hasIntersection = true;
					}
					num2++;
				}
				num++;
			}
			return this._hasIntersection;
		}

		private readonly LineIntersector li = new RobustLineIntersector();

		private bool _hasIntersection;

		private readonly Coordinate pt00 = new Coordinate();

		private readonly Coordinate pt01 = new Coordinate();

		private readonly Coordinate pt10 = new Coordinate();

		private readonly Coordinate pt11 = new Coordinate();
	}
}
